<?php
namespace app\bo\controller;
use think\Controller;
use think\Db;
class Index extends Base
{
    public function index()
    {
        extract(input());
        $where = [
            's_verify' => 2,
            's_type' => 2
        ];
        if(isset($keyword) && $keyword){
            $keyword = strtolower($keyword);
            $where['g_name|g_price|g_text'] = ['like','%'.$keyword.'%'];
        }else{
            $keyword = '';
        }
        $good = Db::table('up_shop')
            ->where($where)
            ->order('s_time desc')
            ->page(1,12)
            ->select();
        $this->assign('good',$good);
        $this->assign('keyword',$keyword);
        return $this->fetch('index/index');
    }

    /**
     * 根据街道ID获取商铺
     * @return \think\response\Json
     */
    public function getShopsById()
    {
        extract(input());
        $id = explode('-',$id);
        $where = [
            's_verify' => 2,
            's_qu' => $id[2],
            's_type' => 2
        ];
        $data = Db::table('up_shop')
            ->where($where)
            ->field('s_id,s_name,s_phone,s_status,s_lon,s_lat,s_address')
            ->select();
        if(empty($data)){
            $data = '';
        }
        return json(['status' => 200,'msg' => $data]);
    }
    //------------------------生成省市区街道的json---------------------------
    public function areaJson()
    {
        set_time_limit(0);
        $one = Db::table('up_area')
            ->where('level', 1)
//            ->where('district', 'in','江苏省,安徽省')
            ->field('district_id CountryId,district CountryName')
//            ->page(1,2)
            ->select();
//        dump($one);die;
        foreach ($one as $k => $v){
            $two = Db::table('up_area')
                ->where('level', 2)
                ->where('pid', $v['CountryId'])
                ->field('district_id ProvinceId,district ProvinceName')
                ->select();
            foreach ($two as $k3 => $v3){
                $three = Db::table('up_area')
                    ->where('level', 3)
                    ->where('pid', $v3['ProvinceId'])
                    ->field('district_id CityId,district CityName')
                    ->select();
                foreach ($three as $k4 => $v4){
                    $four = Db::table('up_area')
                        ->where('level', 4)
                        ->where('pid', $v4['CityId'])
                        ->field('district_id CountyId,district CountyName')
                        ->select();
                    $three[$k4]['CountyList'] = $four;
                }
                $two[$k3]['City'] = $three;
            }
            $one[$k]['Province'] = $two;
        }
        $root = $_SERVER['DOCUMENT_ROOT'];
        $myfile = fopen("$root/static/area.json", "w") or die("Unable to open file!");
        $txt = json_encode($one);
        fwrite($myfile, $txt);
        fclose($myfile);
        echo 'ok';
    }
}
